function getChildren(node) {
    checked = node.parentNode.getUI().isChecked();
    node.getUI().toggleCheck(checked);
    node.eachChild(getChildren);
}

function checkParent(node) {
    parentNodes = node.parentNode;
    if (parentNodes) {
        var allChildren = parentNodes.childNodes;
        // check all child has checked
        var flag = true;
        for(var idx = 0; idx < allChildren.length; idx++) {
            var thisChild = allChildren[idx];
            flag = thisChild.getUI().isChecked();
            if (!flag)
                break;
        }
        
        // all children has checked => check parent
        if (flag) {
            parentNodes.getUI().toggleCheck(true);
            checkParent(parentNodes);
        }
    }
}

Ext.namespace('Ext.Vnf');

Ext.Vnf.CategoryTree = function(rootId, DD, dropToLeaf, config) {
    this.rootId = rootId?rootId:0;
    this.DD = DD;
    this.dropToLeaf = dropToLeaf;
    this.currentId = 0;
    this.currentPath = "";
    this.updating = false;
    
    this.init();
    // call parent constructor
    Ext.Vnf.CategoryTree.superclass.constructor.call(this, config);
   
    this.addListener("click", function(node, e) {
        this.currentId = node.id;
        
        var arrNode = node.getPath().split("/");
        
        var idx = 0;
        this.currentPath = "";
        for(var idx = 2; idx < arrNode.length; idx++) {
            var thisNode = this.getNodeById(arrNode[idx]);
            
            if (idx < arrNode.length - 1)
                this.currentPath += thisNode.text + " >> ";
            else
                this.currentPath += thisNode.text;
        }
    });

/*    this.on("beforeload", function(node) {
        if (node.attributes.name == 'check') {
            checkParent(node);
        }
        
    });*/
    
    this.addListener("checkchange", function(node, checked) {
        this.currentId = node.id;
        node.eachChild(getChildren);
        if (!checked) {
//        alert(node.getPath());
            var arrNode = node.getPath().split("/");
            
            var idx = 0;
            for(var idx = 2; idx < arrNode.length; idx++) {
                var thisNode = this.getNodeById(arrNode[idx]);
                thisNode.getUI().toggleCheck(checked);
            }
        }
        
        if (!checked) {
//        alert(node.getPath());
            var arrNode = node.getPath().split("/");
            
            var idx = 0;
            for(var idx = 2; idx < arrNode.length; idx++) {
                var thisNode = this.getNodeById(arrNode[idx]);
                thisNode.getUI().toggleCheck(checked);
            }
        }
        
        // check parent if all children has checked
        checkParent(node);
    });    
    
};

// end of Ext.ux.CategoryTree constructor

// extend
Ext.extend(Ext.Vnf.CategoryTree, Ext.tree.TreePanel,
{
    autoScroll:true,
    animate:true,
    containerScroll: true,
    baseParams: {lib:'yui'},

    renderTo: Ext.getBody(),

    root: {
        nodeType: 'async',
        text: '',
        draggable:true,
        id:'source-tree-category'
    },
    rootVisible:false,
    callback: function(fn) {
        fn(this.currentId, this.currentPath);
    },
    
    init : function (){
        //this.dataUrl()
        this.enableDD = this.DD;
        this.dataUrl = 'blog/admin/get-category-tree/root/' + this.rootId + '/dd/' + this.dropToLeaf;
    }
    

}); // end of extend

Ext.override(Ext.tree.TreeNodeUI, {
    toggleCheck : function(value){
        //alert(value);
        var cb = this.checkbox;
        if(cb){
            cb.checked = (value === undefined ? !cb.checked : value);
            
        }
        
    },
    
    onCheckChange : function(){
        var checked = this.checkbox.checked;
		// fix for IE6
		this.checkbox.defaultChecked = checked;
        this.node.attributes.checked = checked;
        this.fireEvent('checkchange', this.node, checked);
    }
});